package com.alibaba.citrus.cr.platform.enhance.facade.sls;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import com.aliyun.openservices.loghub.client.ClientWorker;
import com.aliyun.openservices.loghub.client.config.LogHubConfig;
import com.aliyun.openservices.loghub.client.exceptions.LogHubClientWorkerException;
import com.aliyun.openservices.loghub.client.interfaces.ILogHubProcessorFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

/**
 * @author: qiaojiange
 * @create: 2021/11/09
 * @Description 客户工作Bean
 */
@Component
public class ClientWorkerBean implements InitializingBean {
    @Resource
    private ILogHubProcessorFactory iLogHubProcessorFactory;

    @Resource
    private LogHubConfig logHubConfig;

    private ClientWorker clientWorker;

    @Resource
    private SlsConfig slsConfig;

    @Override
    public void afterPropertiesSet() throws Exception {
        if (StringUtils.isBlank(slsConfig.getSEndpoint())) {
            return;
        }
        try {
            this.clientWorker = new ClientWorker(iLogHubProcessorFactory, logHubConfig);
            Thread thread = new Thread(this.clientWorker);
            thread.start();
        } catch (LogHubClientWorkerException e) {
            e.printStackTrace();
        }
    }

    @PostConstruct
    public void init() {

    }

    @PreDestroy
    public void shutdown() {
        if (this.clientWorker != null) {
            this.clientWorker.shutdown();
        }
    }


}
